package com.ecity.table 
{
	import com.ecity.events.GunEvent;
	import com.ecity.util.BitmapClip;
	import com.ecity.util.UIFactory;
	import flash.display.Bitmap;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import gs.TweenLite;
	import gs.easing.*;
	/**
	 * ...
	 * @author yicheng.zhang
	 */
	public class OcelotWheel extends TableDisplayObject {
		
		private var wheel:Bitmap;
		private var bullet:Bitmap;
		
		private var layer:Sprite;
		private var angle:int;
		private var mark:int;
		private var explode:int = 0;
		
		public function OcelotWheel() {
			wheel = UIFactory.getBitmapbyName("wheel") as Bitmap;
			wheel.x = -1*(wheel.width / 2);
			wheel.y = -1*(wheel.height / 2);
			layer = this.getLayer("wheel");
			layer.addChild(wheel);
			appear();
			angle = 0;
			initBullet();
		}
		
		private function initBullet():void {
			bullet = UIFactory.getBitmapbyName("bullet") as Bitmap;
			bullet.x = -20;
			bullet.y = -85;
			bullet.scaleX = 6;
			bullet.scaleY = 6;
			bullet.alpha = 0;
			layer.addChild(bullet);
		}
		
		public function loadBullet():void {
			TweenLite.to(bullet, 1, {scaleX:1, scaleY:1,alpha:1,onComplete:spin});
		}
		
		private function spin():void {
			mark = Math.floor(6 * Math.random());
			var spinAngle:int = 2880 +60*mark;
			TweenLite.to(layer, 2, {rotation:spinAngle,onComplete:onFinishSpin});
		}
		
		private function onFinishSpin():void {
			this.dispatchEvent(new GunEvent(GunEvent.SPIN_COMPLETE));
		}
		
		public function fire():Boolean {
			explode++;
			angle = layer.rotation + 60;
			TweenLite.to(layer, 0.5, { rotation:angle, onComplete:onFinishFire } );
			var isExplode:Boolean = (explode==6-mark);
			return isExplode;
		}
		
		private function onFinishFire():void {
			trace(layer.rotation);
		}
		
		public function appear():void {
			layer.scaleX = 0;
			layer.scaleY = 0;
			TweenLite.to(layer, 0.5, { scaleX:1, scaleY:1, ease:Back.easeIn, onComplete:ocelotIsready } );
			
		}
		
		private function ocelotIsready():void {
			this.dispatchEvent(new GunEvent(GunEvent.WHEEL_ISREADY));
		}
		
		
				
	}

}